<template>
  <div class="layout admin-main">
    <Layout :style="{height: '100%', backgroundColor: '#424242'}">

      <Header :style="{backgroundColor: '#2E2E2E', boxShadow: '0px 0px 6px #000'}">
        <div class="logo-wrap">
          <h1>
            {{ sysName }}
          </h1>
<!--          <span class="version">{{ sysVersion }}</span>-->
        </div>
        <div class="float-right">
          <Dropdown :transfer="false" placement="bottom-start" trigger="click" @on-click="userDropdownClickHandle">
            <Button type="text">
              <Icon type="ios-contact" size="17"/>
              {{ $PnUtil.getCurrentUser() ? $PnUtil.getCurrentUser().username : '' }}
              <Icon type="arrow-down-b"></Icon>
            </Button>
            <DropdownMenu slot="list">
              <DropdownItem name="changePwd">修改密码</DropdownItem>
              <DropdownItem name="logout">退出系统</DropdownItem>
            </DropdownMenu>
          </Dropdown>

          <!--<Dropdown placement="bottom-start" trigger="click">
            <Button type="default">
              切换语言: {{currentLanguage}}
              <Icon type="arrow-down-b"></Icon>
            </Button>
            <DropdownMenu slot="list">
              <DropdownItem v-for="(item,key) in language" :key="item.id" @click.native="changeLanguage(key)">
                {{ item }}
              </DropdownItem>
            </DropdownMenu>
          </Dropdown>-->
        </div>
        <div class="clearfix"></div>
      </Header>

      <Sider :style="{position: 'fixed', height: 'calc(100% - 60px)', left: 0, top: '60px', overflow: 'auto', backgroundColor: '#424242'}">
        <Menu :active-name="menuActiveName" :open-names="menuOpenNames" theme="dark" width="auto">
          <MenuItem name="ProjectManage" to="/admin/project_manage">
            <Icon type="ios-folder-open" size="18"/>
            我的工程
          </MenuItem>
          <Submenu name="my-enshrine">
            <template slot="title">
              <Icon type="md-heart" style="margin-left: -1px;" size="18"/>
              我的收藏
            </template>
            <MenuItem name="PageTemplateManage" to="/admin/page_template_manage">模板收藏</MenuItem>
            <MenuItem name="EnshrineCompManage" to="/admin/enshrine_comp_manage">组件收藏</MenuItem>
          </Submenu>
          <MenuItem v-permission name="CompinfoManage" to="/admin/compinfo_manage">
            <Icon type="md-cube" size="18"/>
            组件库管理
          </MenuItem>
          <MenuItem name="EchartThemeManage" to="/admin/echart_theme_manage">
            <Icon custom="iconfont icon-theme-beautify" size="18"/>
            主题管理
          </MenuItem>
          <Submenu name="database-submenu">
            <template slot="title">
              <Icon custom="iconfont icon-datasource" size="15"/>
              我的数据
            </template>
            <MenuItem name="DatabaseManage" to="/admin/database_manage">数据库</MenuItem>
            <MenuItem name="CsvDatasourceManage" to="/admin/csv_datasource_manage">CSV数据源</MenuItem>
          </Submenu>
          <MenuItem name="ResourceManage" to="/admin/resource_manage">
            <Icon type="md-filing" size="18"/>
            我的资源库
          </MenuItem>
          <MenuItem v-permission name="UserManage" to="/admin/user_manage">
            <Icon type="ios-contacts" style="margin-left: -2px;" size="21"/>
            用户管理
          </MenuItem>
          <MenuItem v-permission name="AttachmentManage" to="/admin/attachment_manage">
            <Icon type="md-folder" style="margin-left: -2px;" size="20"/>
            附件目录管理
          </MenuItem>
          <MenuItem v-permission name="MapGeoJsonManage" to="/admin/mapGeoJson_manage">
            <Icon type="md-map" size="20" style="margin-left: -2px;"/>
            地图源数据管理
          </MenuItem>
          <Submenu name="monitoring-submenu">
            <template slot="title">
              <Icon type="md-videocam" size="16"/>
              系统监控
            </template>
            <MenuItem v-permission name="LogLoginManage" to="/admin/log_login_manage">登录日志</MenuItem>
            <MenuItem name="LogOperateManage" to="/admin/log_operate_manage">操作日志</MenuItem>
          </Submenu>
<!--          <MenuItem name="ChangeLog" to="/admin/change-log">更新日志
            <Tag color="success">20230522</Tag>
          </MenuItem>-->
        </Menu>
      </Sider>

      <Layout :style="{marginLeft: '200px'}">
        <Content :style="{padding: '0px', marginTop: '60px', minHeight: '280px'}">
          <div style="width: 100%; height: 100%; overflow: auto; padding: 15px;">
            <router-view/>
          </div>
        </Content>
      </Layout>

    </Layout>

    <ChangePasswordModal :show="changePwdModalVisible"
                         @cancel="changePwdModalVisible = false"></ChangePasswordModal>

  </div>

</template>

<script>
import Vue from 'vue'
import ChangePasswordModal from "./components/admin/user/ChangePasswordModal";

export default {
  name: 'Admin',
  components: {ChangePasswordModal},
  data() {
    return {
      sysName: window.g.SYS_NAME,
      sysVersion: window.g.SYS_VERSION,

      currentLanguage: "",
      language: {
        "zh-CN": "简体中文",
        "en-US": "英文"
      },

      menuActiveName: '',
      menuOpenNames: [],
      documentationModalVisible: false,
      changePwdModalVisible: false,
    }
  },
  mounted() {
    this.currentLanguage = this.language[Vue.config.lang];

    this.menuActiveName = this.$route.name;
  },
  methods: {
    changeLanguage: function (key) {
      Vue.config.lang = key;
      this.currentLanguage = this.language[key];
      localStorage.setItem('language', key);
    },

    userDropdownClickHandle(name) {
      if (name == 'logout') {
        this.logout();
      } else if (name == 'changePwd') {
        this.changePwdModalVisible = !this.changePwdModalVisible
      }
    },

    logout: function () {
      this.$Modal.confirm({
        title: '提醒',
        content: '确认要退出系统吗？',
        onOk: () => {

          this.$PnApi.AuthApi.logout(localStorage.getItem('token')).then(result => {
            if (result.data.success) {
              localStorage.removeItem('token');
              localStorage.removeItem('current_user');
              this.$router.push('/login')
            } else {
              this.$Message.error(result.data.message)
            }
          });

        }
      });
    }
  },
  computed: {
    databaseSubmenuOpenNames() {
      if (this.menuActiveName == 'DatabaseManage' || this.menuActiveName == 'CsvDatasourceManage') {
        console.log(this.menuActiveName)
        return ['1']
      }
      return ['']
    }
  },
  watch: {
    $route: {
      handler: function (val) {
        if (val.name == 'DatabaseManage' || val.name == 'CsvDatasourceManage') {
          this.menuOpenNames.push('database-submenu')
        }
        if (val.name == 'PageTemplateManage' || val.name == 'EnshrineCompManage') {
          this.menuOpenNames.push('my-enshrine')
        }
        if (val.name == 'LogLoginManage') {
          this.menuOpenNames.push('monitoring-submenu')
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style scoped>
>>> .ivu-menu-child-item-active .ivu-menu-submenu-title {
  background-color: #212121 !important;
}

>>> .ivu-menu-submenu-title {
  color: #ffffff !important;
}

>>> .ivu-menu-submenu .ivu-menu .ivu-menu-item:not(.ivu-menu-item-active):hover {
  background-color: #212121 !important;
}

>>> .ivu-menu-submenu .ivu-menu {
  box-shadow: 0px 0px 10px #212121 inset;
}

>>> .ivu-menu-submenu-title:hover {
  background: #212121 !important;
}
</style>

<style lang="less" scoped>
.layout {
  height: 100%;
  position: relative;
  border-radius: 4px;
  overflow: auto;
}

.logo-wrap {
  width: 200px;
  height: 60px;
  position: relative;
  display: inline-block;
}

.logo-wrap h1 {
  text-align: center;
  color: #fff;
  line-height: 60px;
}

.logo-wrap .version {
  position: absolute;
  right: -20px;
  top: 5px;
  width: auto;
  height: 24px;
  line-height: 24px;
  background-color: #ff9800;
  color: #fff;
  padding: 0px 5px 0px 5px;
  z-index: 99;
  border-radius: 4px;
}

/* 重置iview的相关样式 */
.ivu-menu-item {
  color: #fff !important;

  &:hover {
    background-color: #212121 !important;
  }

  i {
    margin-top: -3px;
  }
}

.ivu-menu-item-selected {
  background: #2196f3 !important;
}

.ivu-layout-header {
  padding: 0px 20px 0px 0px !important;
  line-height: 60px !important;
  background: #424242;
  height: 60px;
  position: fixed;
  width: 100%;
  z-index: 999;
}

</style>
